<?php

namespace app\tool;

class Convert
{
    public static function convert($ws, $fd, $param, $event)
    {
        switch ($param['convertType']) {
                //pdf转png
            case 'pdf_2_png':
                $extention = pathinfo($param['name'])['extension'];
                if ($extention != 'pdf') {
                    $ws->to($fd)->emit('error', '文件格式错误', '文件格式错误');
                    die;
                }

                $fileName    = $param['name'];
                $pdfFilePath = public_path() . '/uploads/convert/' . $fileName;
                //将上传的二进制数据存到本地
                file_put_contents($pdfFilePath, $event->data);

                $pngFileName = time() . ".png";
                $pngFilePath = public_path() . '/uploads/png/' . $pngFileName;

                //将pdf转成png
                exec("convert -density 300 $pdfFilePath -append $pngFilePath");

                //删除pdf文件
                exec("rm -rf $pdfFilePath");

                $ws->to($fd)->emit('callback', 'http://8.130.18.175/uploads/png/' . $pngFileName, $pngFileName);
                break;
                //doc转pdf
            case 'doc_2_pdf':
                $extention = pathinfo($param['name'])['extension'];
                if (!in_array($extention, ['doc', 'docx'])) {
                    $ws->to($fd)->emit('error', '文件格式错误', '文件格式错误');
                    die;
                }

                $fileName    = $param['name'];
                $docFilePath = public_path() . '/uploads/convert/' . $fileName;
                //将上传的二进制数据存到本地
                file_put_contents($docFilePath, $event->data);

                $pdfFileName = time() . ".pdf";
                $pdfFilePath = public_path() . '/uploads/pdf/' . $pdfFileName;
                exec("unoconv pdf $pdfFilePath");
                exec("unoconv -f pdf -o $pdfFilePath $docFilePath");

                exec("rm -rf $docFilePath");
                $ws->to($fd)->emit('callback', 'http://8.130.18.175/uploads/pdf/' . $pdfFileName, $pdfFileName);
                break;
                //图片格式转换
            case 'img':
                $extention = pathinfo($param['name'])['extension'];
                if (!in_array($extention, ['jpg', 'png', 'gif', 'bmp', 'jpeg'])) {
                    $ws->to($fd)->emit('error', '文件格式错误1', '文件格式错误1');
                    die;
                }
                if (!in_array($param['toImgFormat'], ['jpg', 'png', 'gif', 'bmp', 'jpeg'])) {
                    $ws->to($fd)->emit('error', '文件格式错误2', '文件格式错误2');
                    die;
                }

                $fileName       = $param['name'];
                $oldImgFilePath = public_path() . '/uploads/convert/' . $fileName;
                //将上传的二进制数据存到本地
                file_put_contents($oldImgFilePath, $event->data);

                $newImgFileName = time() . "." . $param['toImgFormat'];
                $newImgFilePath = public_path() . '/uploads/img/' . $newImgFileName;

                exec("convert $oldImgFilePath $newImgFilePath");

                exec("rm -rf $oldImgFilePath");
                $ws->to($fd)->emit('callback', 'http://8.130.18.175/uploads/img/' . $newImgFileName, $newImgFileName);
                break;
            case 'imgCompress':
                $extention = pathinfo($param['name'])['extension'];
                if (!in_array($extention, ['jpg', 'png', 'gif', 'bmp', 'jpeg'])) {
                    $ws->to($fd)->emit('error', '文件格式错误', '文件格式错误');
                    die;
                }

                $compressPersent = $param['compressPersent'];
                $compressType = $param['compressType'];; //y压缩类型dbl-等比例压缩，ys-有损压缩

                $fileName       = $param['name'];
                $oldImgFilePath = public_path() . '/uploads/convert/' . $fileName;
                //将上传的二进制数据存到本地
                file_put_contents($oldImgFilePath, $event->data);

                $newImgFileName = time() . "." . $extention;
                $newImgFilePath = public_path() . '/uploads/img/' . $newImgFileName;

                $exec = '';
                if ($compressType == 'ys') {
                    $exec = "convert $oldImgFilePath -quality $compressPersent $newImgFilePath";
                }

                if ($compressType == 'dbl') {
                    $exec = "convert $oldImgFilePath -resize {$compressPersent}% $newImgFilePath";
                }
                exec($exec);

                exec("rm -rf $oldImgFilePath");
                $ws->to($fd)->emit('callback', 'http://8.130.18.175/uploads/img/' . $newImgFileName, $newImgFileName);
                break;
            case 'video':
                $extention = pathinfo($param['name'])['extension'];
                if (!in_array($extention, ['mp4', 'avi', 'mkv', 'flv', 'wmv', 'mov', 'asf', 'm4v', 'f4v'])) {
                    $ws->to($fd)->emit('error', '文件格式错误1', '文件格式错误1');
                    die;
                }
                if (!in_array($param['toVideoFormat'], ['mp4', 'avi', 'mkv', 'flv', 'wmv', 'mov', 'asf', 'm4v', 'f4v'])) {
                    $ws->to($fd)->emit('error', '文件格式错误2', '文件格式错误2');
                    die;
                }

                $compressPersent = $param['compressPersent'];

                $fileName         = $param['name'];
                $oldVideoFilePath = public_path() . '/uploads/convert/' . $fileName;
                //将上传的二进制数据存到本地
                file_put_contents($oldVideoFilePath, $event->data);

                $newVideoFileName = time() . "." . $param['toVideoFormat'];
                $newVideoFilePath = public_path() . '/uploads/video/' . $newVideoFileName;
                exec("ffmpeg -i $oldVideoFilePath $newVideoFilePath");


                exec("rm -rf $oldVideoFilePath");
                $ws->to($fd)->emit('callback', 'http://8.130.18.175/uploads/video/' . $newVideoFileName, $newVideoFileName);
                break;
            case 'videoCompress':
                $extention = pathinfo($param['name'])['extension'];
                if (!in_array($extention, ['mp4', 'avi', 'mkv', 'flv', 'wmv', 'mov', 'asf', 'm4v', 'f4v'])) {
                    $ws->to($fd)->emit('error', '文件格式错误1', '文件格式错误1');
                    die;
                }
                $crf = $param['crf'];

                $fileName         = $param['name'];
                $oldVideoFilePath = public_path() . '/uploads/convert/' . $fileName;
                //将上传的二进制数据存到本地
                file_put_contents($oldVideoFilePath, $event->data);

                $newVideoFileName = time() . "." . $extention;
                $newVideoFilePath = public_path() . '/uploads/compressVideo/' . $newVideoFileName;

                // 准备参数数组
                $args = [$oldVideoFilePath, $crf, $newVideoFilePath];
                // 将参数转换为字符串
                $arguments = implode(' ', $args);
                // 执行Shell脚本并传递参数
                $command = public_path() . "/ffm.sh $arguments";
                $output  = shell_exec($command);
                exec("rm -rf $oldVideoFilePath");

                $ws->to($fd)->emit('callback', 'http://8.130.18.175/uploads/compressVideo/' . $newVideoFileName, $newVideoFileName);
                break;
        }
    }
}
